
function letterCombinations (digits: string): string[] {
    if (digits === "") return [];
    var digit2Letters: { [k: string]: string[] } = {
        '1': new Array<string>(),
        '2': ['a', 'b', 'c' ],
        '3': [ 'd', 'e', 'f' ],
        '4': [ 'g', 'h', 'i' ],
        '5': [ 'j', 'k', 'l' ],
        '6': [ 'm', 'n', 'o' ],
        '7': [ 'p', 'q', 'r', 's' ],
        '8': [ 't', 'u', 'v' ],
        '9': [ 'w', 'x', 'y', 'z' ],
        '0': [ ' ' ]
    };

    var result: string[] = [];
    var tails = letterCombinations(digits.substring(1));
    for(var x of digit2Letters[digits.substring(0,1)]) {
        if (tails.length > 1) {
            for(var t of tails) {
                result.push(x + t);
            }
        }
        else  
             result.push(x);
    }
    return result;
};

console.assert( letterCombinations( "23").sort().join( ",") == ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].sort().join(","))

console.log("happy end")